<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="XEMLSchema" targetNamespace="http://www.mpimp-golm.mpg.de/2007/XEML" elementFormDefault="qualified" xmlns="http://www.mpimp-golm.mpg.de/2007/XEML" xmlns:xeml="http://www.mpimp-golm.mpg.de/2007/XEML" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0.0">
  <xs:element name="XEMLDocument">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Resources" minOccurs="0" maxOccurs="1" type="TResources" />
        <xs:element name="Experiment" minOccurs="1" maxOccurs="1" type="TExperiment">
          <xs:key name="Experiment-Individual_Key">
            <xs:selector xpath=".//xeml:Individual" />
            <xs:field xpath="@Id" />
          </xs:key>
          <xs:unique name="ObservationPoint_Key">
            <xs:selector xpath=".//xeml:ObservationPoint" />
            <xs:field xpath="@Id" />
          </xs:unique>
          <xs:unique name="DestructiveObservation_Key">
            <xs:selector xpath=".//xeml:DestructiveObservation" />
            <xs:field xpath="@individual" />
          </xs:unique>
        </xs:element>
        <xs:element name="Data" type="TDataMapping" minOccurs="0" maxOccurs="1" />
      </xs:sequence>
    </xs:complexType>
    <!--Obsolet-->
    <!--<xs:unique name="ObservationContextConstraint">
      <xs:selector xpath=".//xeml:ObservationContext" />
      <xs:field xpath="@Context" />
    </xs:unique>-->
    <xs:key name="Experiment-Sample_Key">
      <xs:selector xpath=".//xeml:Sample" />
      <xs:field xpath="@Id" />
    </xs:key>
    <xs:key name="Experiment-EnvOnt_Key">
      <xs:selector xpath=".//xeml:EnvironmentalOntology" />
      <xs:field xpath="@NS" />
    </xs:key>
    
    <xs:key name="Experiment-GermplasmOnt_Key">
      <xs:selector xpath=".//xeml:GermPlasmOntology" />
      <xs:field xpath="@NS" />
    </xs:key>
    <xs:key name="Experiment-OrgStructureOnt_Key">
      <xs:selector xpath=".//xeml:OrganismStructureOntology" />
      <xs:field xpath="@NS" />
    </xs:key>
    <xs:key name="Experiment-SampleProvider_Key">
      <xs:selector xpath=".//xeml:DataProvider" />
      <xs:field xpath="@Name" />
    </xs:key>
  </xs:element>
  <xs:complexType name="TXemlAnnotatableObject" abstract="true">
    <xs:sequence>
      <xs:element name="Annotation" type="TAnnotation" minOccurs="0" maxOccurs="unbounded" />
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="TAnnotation">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute name="Tag" type="xs:string" />
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="TExperiment">
    <xs:sequence>
      <xs:element name="Header" type="THeaderData" minOccurs="0" maxOccurs="1" />
      <xs:element name="Story" type="TStory" minOccurs="1" maxOccurs="unbounded">
        <xs:key name="Story-Individual_Key">
          <xs:selector xpath=".//xeml:Individual" />
          <xs:field xpath="@Id" />
        </xs:key>
        <xs:unique name="Story-IndividualsPool_Key">
          <xs:selector xpath=".//xeml:IndividualsPool" />
          <xs:field xpath="@NS" />
          <xs:field xpath="@GermPlasm" />
        </xs:unique>
        <xs:unique name="Story-Variable_Key">
          <xs:selector xpath="./xeml:Variable" />
          <xs:field xpath="@NS" />
          <xs:field xpath="@Name" />
        </xs:unique>
        <xs:unique name="Story-VariableId_Key">
          <xs:selector xpath="./xeml:Variable" />
          <xs:field xpath="@NS" />
          <xs:field xpath="@TermId" />
        </xs:unique>
        <!--<xs:key name="Story-Partition_Key">
          <xs:selector xpath=".//xeml:Partition" /> 
          <xs:field xpath="@Id" />
        </xs:key>-->
        <xs:key name="Story-Partition_Key">
          <xs:selector xpath="./xeml:ObservationPoint/xeml:DestructiveObservation/xeml:Partition|./xeml:ObservationPoint/xeml:Observation/xeml:Partition" />
          <xs:field xpath="@Id" />
        </xs:key>
        <xs:keyref name="s" refer="Story-Partition_Key">
          <xs:selector xpath="./xeml:Sample/xeml:BioSource" />
          <xs:field xpath="@partition" />
        </xs:keyref>
      </xs:element>
    </xs:sequence>
    <xs:attribute name="Name" type="xs:token" use="required" />
    <xs:attribute name="Id" type="TGuid" use="required" />
    <xs:attribute name="StartDate" type="xs:dateTime" />
    <xs:attribute name="ObservationTime" type="TRelativeTime" use="required" />
  </xs:complexType>
  <xs:complexType name="TExperimenter">
    <xs:sequence />
    <xs:attribute name="FirstName" type="xs:token" use="optional" />
    <xs:attribute name="LastName" type="xs:token" use="optional" />
    <xs:attribute name="Email" type="xs:string" use="optional" />
    <xs:attribute name="Organisation" type="xs:string" use="optional" />
  </xs:complexType>
  <xs:complexType name="THeaderData">
    <xs:sequence>
      <xs:element name="Experimenter" type="TExperimenter" minOccurs="0" maxOccurs="1" />
      <xs:element name="Description" type="xs:string" minOccurs="0" maxOccurs="1" />
      <xs:element name="Keyword" type="xs:token" minOccurs="0" maxOccurs="unbounded" />
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="TResources">
    <xs:sequence>
      <xs:sequence minOccurs="0" maxOccurs="unbounded">
        <xs:choice>
          <xs:element name="DataProvider" type="TDataProvider" />
          <xs:element name="EnvironmentalOntology" type="TOntologyHandler" />
          <xs:element name="GermPlasmOntology" type="TOntologyHandler" />
          <xs:element name="OrganismStructureOntology" type="TOntologyHandler" />
          <xs:element name="DevelopmentalOntology" type="TOntologyHandler" />
        </xs:choice>
      </xs:sequence>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="TOntologyHandler">
    <xs:sequence />
    <xs:attribute name="NS" type="xs:Name" use="required" />
    <xs:attribute name="HandlerUri" type="xs:anyURI" />
    <xs:attribute name="InstanceLocation" type="xs:anyURI" use="required" />
  </xs:complexType>
  <xs:complexType name="TDataProvider">
    <xs:sequence />
    <xs:attribute name="ComponentId" type="TGuid" use="optional" />
    <xs:attribute name="Name" type="xs:token" use="required" />
    <xs:attribute name="ProviderUri" type="xs:anyURI" use="required" />
  </xs:complexType>
  <xs:complexType name="TStory">
    <xs:complexContent>
      <xs:extension base="TXemlAnnotatableObject">
        <xs:sequence>
          <xs:element name="IndividualsPool" type="TIndividualsPool" minOccurs="0" maxOccurs="unbounded">
          </xs:element>
          <xs:element name="Variable" type="TVariable" minOccurs="0" maxOccurs="unbounded">
          </xs:element>
          <xs:element name="ObservationPoint" type="TObservationPoint" minOccurs="0" maxOccurs="unbounded">
          </xs:element>
          <xs:element name="Sample" type="TSample" minOccurs="0" maxOccurs="unbounded">
          </xs:element>
          <xs:element name="SuperSample" type="TSuperSample" minOccurs="0" maxOccurs="unbounded">
            <xs:keyref name="SuperSample-Sample_Ref" refer="Experiment-Sample_Key">
              <xs:selector xpath="./xeml:SampleRef">
              </xs:selector>
              <xs:field xpath="@sample" />
            </xs:keyref>
          </xs:element>
          <xs:element name="Event" type="TEvent" minOccurs="0" maxOccurs="unbounded" />
          <xs:element name="StorySplit" type="TStorySplit" minOccurs="0" maxOccurs="unbounded">
            <xs:key name="StorySplit-Partition_Key">
              <xs:selector xpath="./xeml:ObservationPoint/xeml:DestructiveObservation/xeml:Partition|./xeml:ObservationPoint/xeml:Observation/xeml:Partition" />
              <xs:field xpath="@Id" />
            </xs:key>
            <xs:keyref name="s2" refer="StorySplit-Partition_Key">
              <xs:selector xpath="./xeml:Sample/xeml:BioSource" />
              <xs:field xpath="@partition" />
            </xs:keyref>
          </xs:element>
        </xs:sequence>
        <xs:attribute name="Label" type="xs:token" />
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  <xs:complexType name="TStorySplit">
    <xs:complexContent>
      <xs:extension base="TXemlAnnotatableObject">
        <xs:sequence>
          <xs:element name="Variable" type="TVariable" minOccurs="0" maxOccurs="unbounded">
          </xs:element>
          <xs:element name="ObservationPoint" type="TObservationPoint" minOccurs="0" maxOccurs="unbounded">
          </xs:element>
          <xs:element name="Sample" type="TSample" minOccurs="0" maxOccurs="unbounded">
          </xs:element>
          <xs:element name="SuperSample" type="TSuperSample" minOccurs="0" maxOccurs="unbounded" />
          <xs:element name="Event" type="TEvent" minOccurs="0" maxOccurs="unbounded" />
          <xs:element name="StorySplit" type="TStorySplit" minOccurs="0" maxOccurs="unbounded">
            <xs:key name="StorySplit2-Partition_Key">
              <xs:selector xpath="./xeml:ObservationPoint/xeml:DestructiveObservation/xeml:Partition|./xeml:ObservationPoint/xeml:Observation/xeml:Partition" />
              <xs:field xpath="@Id" />
            </xs:key>
            <xs:keyref name="s3" refer="StorySplit2-Partition_Key">
              <xs:selector xpath="./xeml:Sample/xeml:BioSource" />
              <xs:field xpath="@partition" />
            </xs:keyref>
          </xs:element>
        </xs:sequence>
        <xs:attribute name="Label" type="xs:token" />
        <xs:attribute name="TimePoint" type="TRelativeTime" use="required" />
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  <xs:complexType name="TIndividualsPool">
    <xs:complexContent>
      <xs:extension base="TXemlAnnotatableObject">
        <xs:sequence>
          <xs:element name="Individual" type="TIndividual" minOccurs="0" maxOccurs="unbounded" />
        </xs:sequence>
        <xs:attribute name="NS" type="xs:Name" use="optional" default="none" />
        <xs:attribute name="GermPlasm" type="xs:token" use="required" />
        <xs:attribute name="TermId" type="xs:Name" use="optional" />
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  <xs:complexType name="TIndividual">
    <xs:complexContent>
      <xs:extension base="TXemlAnnotatableObject">
        <xs:sequence>
        </xs:sequence>
        <xs:attribute name="Id" type="xs:nonNegativeInteger" use="required" />
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  <xs:complexType name="TVariable">
    <xs:complexContent>
      <xs:extension base="TXemlAnnotatableObject">
        <xs:sequence>
          <xs:element name="ValueSet" type="TValueSet" minOccurs="0" maxOccurs="unbounded" />
          <xs:element name="Tracking" type="TTracking" minOccurs="0" maxOccurs="1" />
        </xs:sequence>
        <xs:attribute name="Name" type="xs:token" use="required" />
        <xs:attribute name="NS" type="xs:Name" default="none" use="optional" />
        <xs:attribute name="GroupPath" type="xs:token" use="optional" />
        <xs:attribute name="TermId" type="xs:Name" use="optional" />
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  <xs:complexType name="TValueSet">
    <xs:sequence>
      <xs:sequence>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element name="Value" type="TValue" />
          <xs:element name="Cycle" type="TCycle" />
        </xs:choice>
      </xs:sequence>
    </xs:sequence>
    <xs:attribute name="TimePoint" type="TRelativeTime" use="required" />
  </xs:complexType>
  <xs:complexType name="TValue">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute name="Context" type="xs:Name" use="required" />
        <xs:attribute name="Unit" type="xs:token" use="optional" />
        <xs:attribute name="Label" type="xs:string" use="optional" />
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="TCycle">
    <xs:sequence>
      <xs:element name="Value" type="TCycleValue" minOccurs="1" maxOccurs="unbounded" />
    </xs:sequence>
    <xs:attribute name="Context" type="xs:Name" use="required" />
    <xs:attribute name="Unit" type="xs:token" use="optional" />
    <xs:attribute name="Label" type="xs:string" use="optional" />
  </xs:complexType>
  <xs:complexType name="TCycleValue">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute name="Duration" type="TRelativeTime" />
        <xs:attribute name="Label" type="xs:string" use="optional" />
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="TTracking">
    <xs:complexContent>
      <xs:extension base="TXemlAnnotatableObject">
        <xs:sequence>
          <xs:element name="Value" type="TTrackingValue" minOccurs="0" maxOccurs="unbounded" />
        </xs:sequence>
        <xs:attribute name="Source" type="xs:string" />
        <xs:attribute name="InitTime" type="xs:dateTime" />
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  <xs:complexType name="TTrackingValue">
    <xs:simpleContent>
      <xs:extension base="TValue">
        <xs:attribute name="Time" type="TRelativeTime" use="required" />
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="TEvent">
    <xs:complexContent>
      <xs:extension base="TXemlAnnotatableObject">
        <xs:sequence>
        </xs:sequence>
        <xs:attribute name="TimePoint" type="TRelativeTime" use="required" />
        <xs:attribute name="Label" type="xs:string" />
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  <xs:complexType name="TObservationPoint">
    <xs:complexContent>
      <xs:extension base="TXemlAnnotatableObject">
        <xs:sequence>
          <xs:sequence>
            <xs:choice minOccurs="0" maxOccurs="unbounded">
              <xs:element name="Observation" type="TObservation">
                <xs:keyref name="Obs_Individual" refer="Story-Individual_Key">
                  <xs:selector xpath="." />
                  <xs:field xpath="@individual" />
                </xs:keyref>
                <xs:keyref name="Obs_IndividualsPools" refer="Story-IndividualsPool_Key">
                  <xs:selector xpath="." />
                  <xs:field xpath="@poolNS" />
                  <xs:field xpath="@poolGermPlasm" />
                </xs:keyref>
              </xs:element>
              <xs:element name="DestructiveObservation" type="TDestructiveObservation">
                <xs:keyref name="DesObs_Individual" refer="Story-Individual_Key">
                  <xs:selector xpath="." />
                  <xs:field xpath="@individual" />
                </xs:keyref>
                <xs:keyref name="DesObs_IndividualsPools" refer="Story-IndividualsPool_Key">
                  <xs:selector xpath="." />
                  <xs:field xpath="@poolNS" />
                  <xs:field xpath="@poolGermPlasm" />
                </xs:keyref>
              </xs:element>
            </xs:choice>
          </xs:sequence>
        </xs:sequence>
        <xs:attribute name="TargetTime" type="TRelativeTime" use="required" />
        <xs:attribute name="Id" type="xs:int" use="required" />
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  <xs:complexType name="TBaseObservation" abstract="true">
    <xs:complexContent>
      <xs:extension base="TXemlAnnotatableObject">
        <xs:sequence>
          <xs:element name="Partition" type="TPartition" minOccurs="0" maxOccurs="unbounded">
          </xs:element>
          <xs:element name="DevelopmentalStage" type="TStage" minOccurs="0" maxOccurs="unbounded">
          </xs:element>
        </xs:sequence>
        <xs:attribute name="individual" type="xs:nonNegativeInteger" use="optional" />
        <xs:attribute name="Duration" type="TRelativeTime" />
        <xs:attribute name="poolNS" type="xs:Name" use="required" />
        <xs:attribute name="poolGermPlasm" type="xs:token" use="required" />
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  <xs:complexType name="TObservation">
    <xs:complexContent>
      <xs:extension base="TBaseObservation">
        <xs:sequence>
        </xs:sequence>
        <xs:attribute name="IndividualInfluence" type="xs:boolean" />
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  <xs:complexType name="TDestructiveObservation">
    <xs:complexContent>
      <xs:extension base="TBaseObservation">
        <xs:sequence />
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  <xs:complexType name="TPartition">
    <xs:complexContent>
      <xs:extension base="TXemlAnnotatableObject">
        <xs:sequence>
          <xs:element name="Material" type="TMaterial" minOccurs="0" maxOccurs="unbounded" />
        </xs:sequence>
        <xs:attribute name="Id" type="xs:nonNegativeInteger" use="required" />
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  <xs:complexType name="TMaterial">
    <xs:complexContent>
      <xs:extension base="TXemlAnnotatableObject">
        <xs:sequence />
        <xs:attribute name="term" type="xs:token" use="required" />
        <xs:attribute name="NS" type="xs:Name" use="optional" default ="none" />
        <xs:attribute name="TermId" type="xs:Name" use="optional" />
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  <xs:complexType name="TStage">
    <xs:complexContent>
      <xs:extension base="TXemlAnnotatableObject">
        <xs:sequence />
        <xs:attribute name="term" type="xs:token" use="required" />
        <xs:attribute name="NS" type="xs:Name" use="optional" default ="none" />
        <xs:attribute name="TermId" type="xs:Name" use="optional" />
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  <xs:complexType name="TSample">
    <xs:complexContent>
      <xs:extension base="TXemlAnnotatableObject">
        <xs:sequence>
          <xs:element name="BioSource" type="TBioSource" minOccurs="0" maxOccurs="unbounded" />
        </xs:sequence>
        <xs:attribute name="Id" type="xs:nonNegativeInteger" />
        <xs:attribute name="TimeIntermix" type="TTimeIntermix" />
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  <xs:complexType name="TSuperSample">
    <xs:complexContent>
      <xs:extension base="TXemlAnnotatableObject">
        <xs:sequence>
          <xs:element name="SampleRef" type="TSampleReference" minOccurs="0" maxOccurs="unbounded" />
        </xs:sequence>
        <xs:attribute name="Id" type="xs:nonNegativeInteger" />
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  <xs:complexType name="TSampleReference">
    <xs:sequence />
    <xs:attribute name="sample" type="xs:nonNegativeInteger" />
  </xs:complexType>
  <xs:complexType name="TBioSource">
    <xs:sequence>
    </xs:sequence>
    <xs:attribute name="partition" type="xs:int" use="required" />
  </xs:complexType>
  <xs:complexType name="TDataMapping">
    <xs:complexContent>
      <xs:extension base="TXemlAnnotatableObject">
        <xs:sequence>
          <xs:element name="SampleMapping" type="TSampleMapping" minOccurs="0" maxOccurs="unbounded">
            <xs:keyref name="SampleMapping-SampleId" refer="Experiment-Sample_Key">
              <xs:selector xpath=".">
              </xs:selector>
              <xs:field xpath="@sample" />
            </xs:keyref>
          </xs:element>
        </xs:sequence>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  <xs:complexType name="TSampleMapping">
    <xs:sequence>
      <xs:element name="Sid" type="TSid" minOccurs="1" maxOccurs="unbounded">
        <xs:keyref name="Sid-Provieder_KeyRef" refer="Experiment-SampleProvider_Key">
          <xs:selector xpath="." />
          <xs:field xpath="@Provider" />
        </xs:keyref>
      </xs:element>
    </xs:sequence>
    <xs:attribute name="sample" type="xs:nonNegativeInteger" use="required" />
  </xs:complexType>
  <xs:complexType name="TSid">
    <xs:complexContent>
      <xs:extension base="TXemlAnnotatableObject">
        <xs:sequence>
          <xs:element name="Subkey" type="TSubKey" minOccurs="0" maxOccurs="unbounded" />
        </xs:sequence>
        <xs:attribute name="Provider" type="xs:token" use="required" />
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  <xs:complexType name="TSubKey">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute name="Name" type="xs:token" />
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:simpleType name="TRelativeTime">
    <xs:restriction base="xs:normalizedString">
      <xs:pattern value="(([1-9][0-9]{0,3}\.){0,1}(([0-1][0-9]|[2][0-3]){1}(:[0-5][0-9]){0,2}))|[1-9][0-9]{0,3}" />
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="TDataType">
    <xs:restriction base="xs:normalizedString">
      <xs:enumeration value="Number" />
      <xs:enumeration value="Text" />
      <xs:enumeration value="Boolean" />
      <xs:enumeration value="Date" />
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="TGuid">
    <xs:restriction base="xs:string">
      <xs:pattern value="\{([a-f]|[A-F]|[0-9]){8}-([a-f]|[A-F]|[0-9]){4}-([a-f]|[A-F]|[0-9]){4}-([a-f]|[A-F]|[0-9]){4}-([a-f]|[A-F]|[0-9]){12}\}" />
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="TTimeIntermix">
    <xs:restriction base="xs:string">
      <xs:enumeration value="Min" />
      <xs:enumeration value="Max" />
      <xs:enumeration value="Mean" />
    </xs:restriction>
  </xs:simpleType>
 
</xs:schema>